package com.eachonline.smart.web.home;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.smart.model.SmartArtcile;
import com.eachonline.smart.model.SmartArtcileExample;
import com.eachonline.smart.model.SmartCourse;
import com.eachonline.smart.model.SmartCourseExample;
import com.eachonline.smart.model.SysCourseCategory;
import com.eachonline.smart.model.SysCourseCategoryExample;
import com.eachonline.smart.model.base.BaseSysCourseCategoryExample.Criteria;
import com.eachonline.smart.service.ArticleService;
import com.eachonline.smart.service.CourseService;
import com.eachonline.smart.service.SysCourseCategoryService;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;

@Controller
@RequestMapping("/home")
public class SmartHomeController {

	@Autowired
	public  SysCourseCategoryService  sysCourseService;
	
	@Autowired
	public  CourseService  courseService;
	
	@Autowired
	public  ArticleService  articleService;

	/**
	 * 课程导航栏
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queyNav",method = RequestMethod.POST)
	public JsonResult queryHomeNacList(HttpServletRequest request,String parentCode){
		
		SysCourseCategoryExample  caExa=new SysCourseCategoryExample();
		Criteria cateCri=caExa.createCriteria();
		if(StringUtil.isNotEmpty(parentCode))
		{
			cateCri.andParentCodeEqualTo(parentCode);
		}
		
		Pagination<SysCourseCategory> pageSys= sysCourseService.queryByPage(caExa, 0, 100, " value asc");
		
		Map<String,Object>  resultMap=new HashMap<String,Object>();
		resultMap.put("navList", pageSys.getDataList());
		return  new JsonResult(true, resultMap);
		
	}
		
	
	/**
	 * 课程导类目详情
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manager/queyNavDetail",method = RequestMethod.POST)
	public JsonResult queryManagerNacList(HttpServletRequest request,String code){
		
		SysCourseCategoryExample  caExa=new SysCourseCategoryExample();
		Criteria cateCri=caExa.createCriteria();
		if(StringUtil.isNotEmpty(code))
		{
			cateCri.andValueEqualTo(code);
		}
		
		List<SysCourseCategory> cateList=sysCourseService.selectByExample(caExa);
		if(cateList.isEmpty())
		{
			return  new JsonResult(false, "类目不存在");
		}
		
		Map<String,Object>  resultMap=new HashMap<String,Object>();
		resultMap.put("sysCate", cateList.get(0));
		return  new JsonResult(true, resultMap);
		
	}
	
	
	/**
	 * 课程导类目添加
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manager/sysCateInsert",method = RequestMethod.POST)
	public JsonResult navSave(HttpServletRequest request,SysCourseCategory sysCate){
		
		SysCourseCategoryExample  caExa=new SysCourseCategoryExample();
		Criteria cateCri=caExa.createCriteria();
		
		if(StringUtil.isNotEmpty(sysCate.getValue()))
		{
			cateCri.andValueEqualTo(sysCate.getValue());
		}else
		{
			return  new JsonResult(false, "类目Code不能为空");
		}
		
		List<SysCourseCategory> cateList=sysCourseService.selectByExample(caExa);
		if(!cateList.isEmpty())
		{
			return  new JsonResult(false, "类目Code已存在");
		}
		
		sysCate.setSid(UUIDUtil.getUUID());
		sysCate.setIsUse("1");
		sysCate.setCreateTime(new Date());
		sysCourseService.insert(sysCate);
		
		return  new JsonResult(true, "成功");
		
	}
	
	

	/**
	 * 课程导类目添加
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manager/sysCateUpdate",method = RequestMethod.POST)
	public JsonResult sysCateUpdate(HttpServletRequest request,SysCourseCategory sysCate){
		
		SysCourseCategoryExample  caExa=new SysCourseCategoryExample();
		
		
		if(StringUtil.isEmpty(sysCate.getSid()))
		{
			return  new JsonResult(false, "编辑类目ID不能为空");
		}
		Criteria cateCri=caExa.createCriteria();
		
		if(StringUtil.isNotEmpty(sysCate.getValue()))
		{
			cateCri.andValueEqualTo(sysCate.getValue()).andSidNotEqualTo(sysCate.getSid());
		}else
		{
			return  new JsonResult(false, "类目Code不能为空");
		}
		
		List<SysCourseCategory> cateList=sysCourseService.selectByExample(caExa);
		if(!cateList.isEmpty())
		{
			return  new JsonResult(false, "类目Code已存在");
		}
		
		sysCourseService.updateByPrimaryKeySelective(sysCate);
		
		return  new JsonResult(true, "成功");
		
	}
	

	
	/**
	 * 课程导类目添加
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/manager/deleteCate",method = RequestMethod.POST)
	public JsonResult deleteCate(HttpServletRequest request,String sid){
		
		if(StringUtil.isEmpty(sid))
		{
			return  new JsonResult(false, "sid不能为空");
		}
		
		SysCourseCategory cate=sysCourseService.selectByPrimaryKey(sid);
		
		if(cate==null)
		{
			return  new JsonResult(false, "类目不存在");
		}
		
	    SysCourseCategoryExample  caExa=new SysCourseCategoryExample();
		Criteria cateCri=caExa.createCriteria();
		cateCri.andParentCodeEqualTo(cate.getValue());
		
		List<SysCourseCategory> cateList=sysCourseService.selectByExample(caExa);
		if(cateList.isEmpty())
		{
			sysCourseService.deleteByPrimaryKey(sid);
			return  new JsonResult(true, "成功");
			
		}
		return  new JsonResult(false, "存在子类目不能删除，需要删除，请先删除子类目");
	}
	
	/**
	 * 首页课程查询接口
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/homeSearch",method = RequestMethod.POST)
	public JsonResult homeSearch(HttpServletRequest request,String name){
		
		if(StringUtil.isEmpty(name))
		{
			return  new JsonResult(false, "搜索内容不能为空！");
		}
	
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
	
		SmartCourseExample  courseExa=new SmartCourseExample();
		com.eachonline.smart.model.base.BaseSmartCourseExample.Criteria   courseCri=courseExa.createCriteria();
		courseCri.andCourseNameLike("%"+name+"%");
		courseCri.andCheckStatusEqualTo("2");
		Pagination<SmartCourse>  courseInfo=courseService.queryByPage(courseExa, begin, pageSize);
		
		SmartArtcileExample artEx=new SmartArtcileExample();
		
		artEx.createCriteria().andArticleNameLike("%"+name+"%").andCheckStatusEqualTo("2");
		
		Pagination<SmartArtcile>  articleInfo=articleService.queryByPage(artEx, begin, pageSize);
		
		Map<String,Object> result=new HashMap<String, Object>();
		result.put("courseInfo", courseInfo);
		result.put("articleInfo", articleInfo);
		return  new JsonResult(true, result);
	
	/*	SmartCourseExample  courseExa=new SmartCourseExample();
		com.eachonline.smart.model.base.BaseSmartCourseExample.Criteria   courseCri=courseExa.createCriteria();
		courseCri.andCourseNameLike("%"+name+"%");
		courseCri.andCheckStatusEqualTo("2");
		List<SmartCourse>  courseList=courseService.selectByExample(courseExa);
		
		SmartArtcileExample artEx=new SmartArtcileExample();
		
		artEx.createCriteria().andArticleNameLike("%"+name+"%").andCheckStatusEqualTo("2");
		
		List<SmartArtcile>  articleList=articleService.selectByExample(artEx);
		
		Map<String,Object> result=new HashMap<String, Object>();
		result.put("courseList", courseList);
		result.put("articleList", articleList);
		return  new JsonResult(true, result);*/
		
	}
}
